package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysUserDepartmentRelateDAO;
import com.ourteam.system.dao.SysUserDepartmentRelate;
import com.ourteam.system.dao.SysUserDepartmentRelateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:40
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysUserDepartmentRelateDAOJdbcImpl
    extends AbstractSpringDAO implements ISysUserDepartmentRelateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, SysUserId, SysDepartmentId, DepartmentRole, Status, SysUserName,
            SysUserAccount, SysUserRemarks, SysUserStatus, SysUserMobilePhone,
            DepartmentName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.SysUserId,
            _DataObjectProperty.SysDepartmentId,
            _DataObjectProperty.DepartmentRole, _DataObjectProperty.Status,
            _DataObjectProperty.SysUserName, _DataObjectProperty.SysUserAccount,
            _DataObjectProperty.SysUserRemarks,
            _DataObjectProperty.SysUserStatus,
            _DataObjectProperty.SysUserMobilePhone,
            _DataObjectProperty.DepartmentName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysUserDepartmentRelate, SysUserDepartmentRelateDAOQueryBean> daoHelper;

    public AbstractSysUserDepartmentRelateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysUserDepartmentRelate, SysUserDepartmentRelateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" SYS_USER ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "SYS_USER."));
            relatedJoinSQLBuffer.append(" = ").append(SysUserId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDepartmentDAO.EntityName)
                                .append(" SYS_DEPARTMENT ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDepartmentDAO.Id,
                    com.ourteam.system.dao.ISysDepartmentDAO.EntityName + ".",
                    "SYS_DEPARTMENT."));
            relatedJoinSQLBuffer.append(" = ").append(SysDepartmentId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysUserDepartmentRelate
         * @throws Exception
         */
    public boolean checkIsUnique(
        SysUserDepartmentRelate aSysUserDepartmentRelate)
        throws Exception {
        if (aSysUserDepartmentRelate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysUserDepartmentRelate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysUserDepartmentRelate
     * @throws Exception
     */
    public void insert(SysUserDepartmentRelate aSysUserDepartmentRelate)
        throws Exception {
        super.doInsert(aSysUserDepartmentRelate);
    }

    /**
         * Batch Insert Datas
         * @param aSysUserDepartmentRelate
         * @throws Exception
         */
    public void batchInsert(SysUserDepartmentRelate[] aSysUserDepartmentRelates)
        throws Exception {
        super.doBatchInsert(aSysUserDepartmentRelates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysUserDepartmentRelateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysUserDepartmentRelate aSysUserDepartmentRelate)
        throws Exception {
        delete(aSysUserDepartmentRelate.getId());
    }

    /**
     * Update Data Selective
     * @param aSysUserDepartmentRelate
     * @throws Exception
     */
    public void update(SysUserDepartmentRelate aSysUserDepartmentRelate)
        throws Exception {
        if (aSysUserDepartmentRelate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysUserDepartmentRelate);

        SysUserDepartmentRelate tempObj = findById(aSysUserDepartmentRelate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysUserDepartmentRelate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysUserDepartmentRelates[]
     * @throws Exception
     */
    public void batchUpdate(SysUserDepartmentRelate[] aSysUserDepartmentRelates)
        throws Exception {
        super.doBatchUpdate(aSysUserDepartmentRelates);
    }

    /**
         * Update Data By Condictions
         * @param aSysUserDepartmentRelate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysUserDepartmentRelate aSysUserDepartmentRelate,
        SysUserDepartmentRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aSysUserDepartmentRelate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysUserDepartmentRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(
        SysUserDepartmentRelate aSysUserDepartmentRelate)
        throws Exception {
        if (aSysUserDepartmentRelate.getSysUserRemarks() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.sysUserRemarks value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getSysUserStatus() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.sysUserStatus value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getDepartmentRole() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.departmentRole value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getStatus() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.status value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getSysUserName() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.sysUserName value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getSysUserAccount() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.sysUserAccount value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getDepartmentName() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.departmentName value is not allow NULL");
        }

        if (aSysUserDepartmentRelate.getSysUserMobilePhone() == null) {
            throw new Exception(
                "The SysUserDepartmentRelate.sysUserMobilePhone value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysUserDepartmentRelate findById(final long id)
        throws Exception {
        SysUserDepartmentRelateDAOQueryBean queryBean = new SysUserDepartmentRelateDAOQueryBean();

        SysUserDepartmentRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysUserDepartmentRelate[] SysUserDepartmentRelates = query(queryBean);

        if (SysUserDepartmentRelates.length == 0) {
            return null;
        } else {
            return SysUserDepartmentRelates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysUserDepartmentRelate[]
         * @throws Exception
         */
    public SysUserDepartmentRelate[] query(
        SysUserDepartmentRelateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcSysUserDepartmentRelateRowMapper());

        return (SysUserDepartmentRelate[]) result.toArray(new SysUserDepartmentRelate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysUserDepartmentRelateDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysUserDepartmentRelateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(
        SysUserDepartmentRelate[] aSysUserDepartmentRelates,
        SysUserDepartmentRelateDAOQueryBean queryBean)
        throws Exception {
        daoHelper.synchronize(aSysUserDepartmentRelates, queryBean);
    }

    public void synchronize(
        SysUserDepartmentRelate[] aSysUserDepartmentRelates,
        SysUserDepartmentRelateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysUserDepartmentRelate> callBack)
        throws Exception {
        daoHelper.synchronize(aSysUserDepartmentRelates, queryBean, callBack);
    }

    public static class JdbcSysUserDepartmentRelateRowMapper
        implements RowMapper<SysUserDepartmentRelate> {
        public SysUserDepartmentRelate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysUserDepartmentRelate object = new SysUserDepartmentRelate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object sysUserId = null;

                sysUserId = resultSet.getObject(columnIndex++);

                object.setSysUserIdNull(resultSet.getBoolean(columnIndex++));

                if (sysUserId != null) {
                    setBeanValue(object, _DataObjectProperty.SysUserId,
                        sysUserId);
                }

                Object sysDepartmentId = null;

                sysDepartmentId = resultSet.getObject(columnIndex++);

                object.setSysDepartmentIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (sysDepartmentId != null) {
                    setBeanValue(object, _DataObjectProperty.SysDepartmentId,
                        sysDepartmentId);
                }

                Object departmentRole = null;

                departmentRole = resultSet.getObject(columnIndex++);

                object.setDepartmentRoleNull(resultSet.getBoolean(columnIndex++));

                if (departmentRole != null) {
                    setBeanValue(object, _DataObjectProperty.DepartmentRole,
                        departmentRole);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sysUserName = null;

                sysUserName = resultSet.getObject(columnIndex++);

                object.setSysUserNameNull(resultSet.getBoolean(columnIndex++));

                if (sysUserName != null) {
                    setBeanValue(object, _DataObjectProperty.SysUserName,
                        sysUserName);
                }

                Object sysUserAccount = null;

                sysUserAccount = resultSet.getObject(columnIndex++);

                object.setSysUserAccountNull(resultSet.getBoolean(columnIndex++));

                if (sysUserAccount != null) {
                    setBeanValue(object, _DataObjectProperty.SysUserAccount,
                        sysUserAccount);
                }

                Object sysUserRemarks = null;

                sysUserRemarks = resultSet.getObject(columnIndex++);

                object.setSysUserRemarksNull(resultSet.getBoolean(columnIndex++));

                if (sysUserRemarks != null) {
                    setBeanValue(object, _DataObjectProperty.SysUserRemarks,
                        sysUserRemarks);
                }

                Object sysUserStatus = null;

                sysUserStatus = resultSet.getObject(columnIndex++);

                object.setSysUserStatusNull(resultSet.getBoolean(columnIndex++));

                if (sysUserStatus != null) {
                    setBeanValue(object, _DataObjectProperty.SysUserStatus,
                        sysUserStatus);
                }

                Object sysUserMobilePhone = null;

                sysUserMobilePhone = resultSet.getObject(columnIndex++);

                object.setSysUserMobilePhoneNull(resultSet.getBoolean(
                        columnIndex++));

                if (sysUserMobilePhone != null) {
                    setBeanValue(object,
                        _DataObjectProperty.SysUserMobilePhone,
                        sysUserMobilePhone);
                }

                Object departmentName = null;

                departmentName = resultSet.getObject(columnIndex++);

                object.setDepartmentNameNull(resultSet.getBoolean(columnIndex++));

                if (departmentName != null) {
                    setBeanValue(object, _DataObjectProperty.DepartmentName,
                        departmentName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
